package com.vvv.designpattern.transaction.mediator;

import com.vvv.designpattern.transaction.colleague.AbstractCustomer;
import com.vvv.designpattern.transaction.colleague.Buyer;
import com.vvv.designpattern.transaction.colleague.Payer;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author vvv
 * @date 2023-11-25 17 07
 * discription中介者模式：
 * Mediator为具体中介者，需要关联所以同事类
 * @Component 全局唯一中介者
 */
@Component
public class Mediator extends AbstractMediator {

    public static final Map<String, Map<String, AbstractCustomer>> customerInstances = new ConcurrentHashMap<>();

    @Override
    public void messageTransfer(String orderId, String targetCustomer, AbstractCustomer customer, String payResult) {
        if (customer instanceof Buyer) {
            AbstractCustomer buyer  = customerInstances.get(orderId).get("buyer");
            System.out.println("朋友代付：" + buyer.getCustomerName() + "转发OrderId " + orderId + " 到用户" + targetCustomer);
        } else if (customer instanceof Payer) {
            AbstractCustomer payer = customerInstances.get(orderId).get("payer");
            System.out.println("代付成功：" + payer.getCustomerName() + "完成OrderId " + orderId + " 的支付。通知" + targetCustomer);
            customerInstances.remove(orderId);
        }
    }
}
